//
//  TWHeaderView.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/3/14.
//  Copyright © 2017年 个人. All rights reserved.
//  头部的视图封装

import UIKit

class TWHeaderView: UIView {

    // MARK:- 懒加载
    lazy var headerImageView : UIImageView = UIImageView()                  // 头部视图中的背景图片
    fileprivate lazy var middleImageView : UIImageView = UIImageView()      // 背景图片中间的圆形图片
    fileprivate lazy var seachButton : UIButton = UIButton()                // 刷新控件
    fileprivate lazy var timeLabel : UILabel = UILabel()                    // 圆形图片下边的时间label
    lazy var blackView : UIView = UIView()                                  // 头部视图中的蒙版层
    lazy var activityIndicatorView = UIActivityIndicatorView()              // 刷新控件

    // MARK:- 系统回调函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.cyan
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        // 背景上的每日精选图片
        middleImageView.center = CGPoint(x: self.frame.width * 0.5, y: self.frame.height * 0.5)
        // 时间
        let centerY : CGFloat = (self.frame.height - (middleImageView.frame.origin.y + middleImageView.frame.height)) * 0.5 + middleImageView.frame.origin.y + middleImageView.frame.height
        timeLabel.center = CGPoint(x: middleImageView.center.x, y: centerY)
        // 菊花
        activityIndicatorView.center = timeLabel.center
    }
}

extension TWHeaderView {
    /// 获得当前的时间
    fileprivate func retureNowDate() -> String{
        let currentdate = Date()
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = "YYYY.MM.dd"
        let dateStr = dateformatter.string(from: currentdate)
        return dateStr
    }
    func coverHeaderView(alpha : CGFloat){
        // 显现模板
        blackView.alpha = alpha
    }
    func prepareForBeginRefresh(offset : CGFloat){
        // 准备开始刷新
        let alpha = offset / refreshingOffset
        timeLabel.alpha = 1 - alpha
        if timeLabel.alpha < 0 {
            timeLabel.isHidden = true
            activityIndicatorView.isHidden = false
            activityIndicatorView.startAnimating()
        }
    }
    func refreshing(){
        // 正在刷新
        timeLabel.isHidden = true
        activityIndicatorView.isHidden = false
        activityIndicatorView.startAnimating()
    }
    func endRefresh(){
        // 停止刷新
        let dateStr = retureNowDate()
        timeLabel.text = "-\(dateStr)-"
        timeLabel.alpha = 1
        timeLabel.isHidden = false
        activityIndicatorView.isHidden = true
        activityIndicatorView.stopAnimating()
    }
}

extension TWHeaderView {
    fileprivate func setupUI(){
        // 背景图片
        headerImageView = UIImageView(frame: self.bounds)
        headerImageView.image = UIImage(named: "Home_header")
        headerImageView.contentMode = .scaleAspectFill
        addSubview(headerImageView)
        
        // 添加中间的每日精选图片 150x136
        middleImageView = UIImageView(image: UIImage(named: "badge_selected_header_150x136_"))
        middleImageView.sizeToFit()
        middleImageView.center = CGPoint(x: self.bounds.width * 0.5, y: self.bounds.height * 0.5)
        addSubview(middleImageView)
        
        // 菊花
        activityIndicatorView.center = timeLabel.center
        activityIndicatorView.hidesWhenStopped = true
        addSubview(activityIndicatorView)
        
        // 添加时间文本
        let dateStr = retureNowDate()
        timeLabel.text = "-\(dateStr)-"
        timeLabel.font = UIFont.init(name: "Lobster1.4", size: 15)
        timeLabel.sizeToFit()
        timeLabel.textAlignment = .center
        timeLabel.textColor = UIColor.white
        addSubview(timeLabel)
        
        // 搜索按钮
        seachButton.setImage(UIImage(named: "bar_ic_search_white_44x44_"), for: .normal)
        seachButton.frame = CGRect(x: UIScreen.main.bounds.width - 44, y: 20, width: 44, height: 44)
        addSubview(seachButton)
        
        // 添加蒙版
        blackView = UIView(frame: self.frame)
        blackView.backgroundColor = UIColor.black
        blackView.alpha = 0
        addSubview(blackView)
    }
}
